package drive.Mapper;

import drive.Model.RoadDisaster;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoadDisasterMapper {

    @Select("SELECT * FROM road_disaster")
    List<RoadDisaster> findAll();

    @Select("SELECT * FROM road_disaster WHERE disasterID = #{disasterID}")
    RoadDisaster findByDisasterID(Integer disasterID);

    @Select("SELECT * FROM road_disaster WHERE userID = #{userID}")
    List<RoadDisaster> findByUserID(Integer userID);

    @Insert("INSERT INTO road_disaster (disasterType, disasterInfo, longitude, latitude, userID, status, video_url) VALUES (#{disasterType}, #{disasterInfo}, #{longitude}, #{latitude}, #{userID}, #{status}, #{video_url})")
    @Options(useGeneratedKeys = true, keyProperty = "disasterID", keyColumn = "disasterID")
    int add(RoadDisaster roadDisaster);

    @Update("UPDATE road_disaster SET disasterType = #{disasterType}, disasterInfo = #{disasterInfo}, longitude = #{longitude}, latitude = #{latitude}, userID = #{userID}, status = #{status}, video_url = #{video_url} WHERE disasterID = #{disasterID}")
    int update(RoadDisaster roadDisaster);

    @Delete("DELETE FROM road_disaster WHERE disasterID = #{disasterID}")
    int delete(Integer disasterID);

    /**
     * 根据经纬度上下限获取灾害信息
     * @param minLatitude 最小纬度
     * @param maxLatitude 最大纬度
     * @param minLongitude 最小经度
     * @param maxLongitude 最大经度
     * @return 灾害信息列表
     */
    @Select("SELECT * FROM road_disaster WHERE latitude BETWEEN #{minLatitude} AND #{maxLatitude} AND longitude BETWEEN #{minLongitude} AND #{maxLongitude} AND status != 'resolved'")
    List<RoadDisaster> getDisastersByLocation(double minLatitude, double maxLatitude, double minLongitude, double maxLongitude);
}